home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / QuickDraw / MakeITable / MakeITable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-15  |  12.0 KB  |  478 lines  |  [TEXT/MPS ]

  1. /*______________________________________________________*/
  2. /*                            C Shell                        */
  3. /*                          by                          */
  4. /*                  RICHARD P. COLLYER                  */
  5. /*                       09/02/87                       */
  6. /*______________________________________________________*/
  7.  
  8. #include    <CType.h>
  9. #include    <Quickdraw.h>
  10. #include    <QDOffscreen.h>
  11. #include    <Windows.h>
  12. #include    <OSUtils.h>
  13. #include    <Controls.h>
  14. #include    <desk.h>
  15. #include    <dialogs.h>
  16. #include    <Events.h>
  17. #include    <Memory.h>
  18. #include    <Menus.h>
  19. #include    <Notification.h>
  20. #include    <OSEvents.h>
  21. #include    <Packages.h>
  22. #include    <Palette.h>
  23. #include    <Printing.h>
  24. #include    <Resources.h>
  25. #include    <SANE.h>
  26. #include    <SegLoad.h>
  27. #include    <Sound.h>
  28. #include    <Slots.h>
  29. #include    <ToolUtils.h>
  30. #include    <StdIO.h>
  31. #include    <math.h>
  32.  
  33. extern _DataInit();
  34.  
  35. #define    TRUE            0xFF
  36. #define    FALSE            0
  37. #define    VERSION            1
  38. #define    SR_BIT            0
  39.  
  40. #define    appleID            128            
  41. #define    appleMenu        0
  42. #define    aboutMeCommand    1
  43.  
  44. #define    fileID            129
  45. #define    startCommand    1
  46. #define    printCommand    3
  47. #define    quitCommand     5
  48.  
  49. #define    editID            130
  50.  
  51. #define    aboutMeDLOG        128
  52. #define    okButton        1
  53. #define    authorItem        2
  54. #define    languageItem    3
  55.  
  56. long                Tick, WhereBoard;
  57. int                    yieldTime, err, numcolor, offLeft, offTop, offRight, offBottom;
  58. Rect                screenRect, BaseRect, TotalRect, minRect;
  59. WindowPtr            whichWindow, myWindow;
  60. CTabHandle            mycolors;
  61. PaletteHandle        srcPalette;
  62. MenuHandle            mymenu1, mymenu2, mymenu0;
  63. EventRecord         myEvent;
  64. Boolean                DoneFlag, Color, CPU20, FPU, Sys6, Memboard;
  65. GDHandle            theGDevice;
  66. SysEnvRec            theWorld;
  67. OSErr                OSys;
  68. SpBlockPtr            mySpBlockPtr;
  69. SpBlock                mySpBlock;
  70.     
  71. /*______________________________________________________*/
  72. /*                What ever You want                    */
  73. /*______________________________________________________*/
  74. void start()
  75. {
  76.     GDHandle    theGDHand;
  77.     
  78.     theGDHand = GetDeviceList();
  79.     do {
  80.         HLock ((Handle) theGDHand);
  81.         if (TestDeviceAttribute (theGDHand, 13) && 
  82.                 TestDeviceAttribute (theGDHand, 15) &&
  83.                 (*theGDHand)->gdType == 0) {
  84.             (*theGDHand)->gdResPref = 5;
  85.             (*(*theGDHand)->gdITable)->iTabSeed = GetCTSeed();
  86.             GDeviceChanged (theGDHand);
  87.             HUnlock ((Handle) theGDHand);
  88.             }
  89.         } while ((theGDHand = GetNextDevice(theGDHand)) != nil);
  90.  
  91.     return;
  92. }
  93.  
  94. /*______________________________________________________*/
  95. /*                     Draw Stuff                       */
  96. /*______________________________________________________*/
  97. void DrawStuff (theRect)
  98.  
  99.     Rect         theRect;
  100. {
  101.     SetRect (&theRect, 10, 10, 200, 200);
  102.     
  103.     CopyBits (&qd.screenBits, &qd.thePort->portBits, &qd.screenBits.bounds, 
  104.                 &qd.screenBits.bounds, srcCopy, nil);
  105.  
  106.  
  107. /**
  108.     CopyBits (&window->portBits, &window->portBits, &window->portRect, &window->portRect, srcOr, nil);
  109.     CopyBits (&windpeek->port.portBits, &windpeek->port.portBits, &window->portRect, &window->portRect, srcOr, nil);
  110.     CopyBits (&screenBits, &(*thePort).portBits, &theWorld, &theWorld, srcOr, nil);
  111. */
  112.     return;
  113. }  /* DrawStuff */
  114.  
  115. /*______________________________________________________*/
  116. /*                        Print                         */
  117. /*______________________________________________________*/
  118. void Print()
  119. {
  120.     TPPrPort    thePrPort;
  121.     TPrStatus    theStatus;
  122.     GrafPtr        oldPort;
  123.     THPrint        thePrRecHdl;
  124.     
  125.     GetPort(&oldPort);
  126.     
  127.     thePrRecHdl = (THPrint)  NewHandle (sizeof (TPrint));
  128.     
  129.     PrOpen();
  130.  
  131.         if (PrError() == noErr)
  132.          {
  133.             PrintDefault(thePrRecHdl);
  134.             
  135.             if (! PrStlDialog(thePrRecHdl)) 
  136.               ExitToShell();
  137.               
  138.             if (! PrJobDialog(thePrRecHdl)) 
  139.               ExitToShell();
  140.               
  141.             thePrPort = PrOpenDoc(thePrRecHdl, nil, nil);
  142.         
  143.             if (PrError() == noErr) 
  144.              {
  145.                 PrOpenPage(thePrPort, nil);
  146.                 if (PrError() == noErr) 
  147.                  {
  148.                     /********* Print from here ****************/
  149.                     
  150.                     DrawStuff ((**thePrRecHdl).prInfo.rPage);
  151.                  }
  152.              }
  153.             PrClosePage(thePrPort);
  154.         }
  155.         PrCloseDoc(thePrPort);
  156.     
  157.     if ((((TPPrint)*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop) && (PrError() == noErr))
  158.         PrPicFile(thePrRecHdl, nil, nil, nil, &theStatus);                
  159.  
  160.     PrClose();
  161.  
  162.     SetPort(oldPort);
  163.     return;
  164. }
  165.  
  166. /*______________________________________________________*/
  167. /*                 About Prog Dialog                    */
  168. /*______________________________________________________*/
  169. void showAboutMeDialog()
  170. {
  171.     GrafPtr     savePort;
  172.     DialogPtr    theDialog;
  173.     short        itemHit;
  174.  
  175.     GetPort(&savePort);
  176.     theDialog = GetNewDialog(aboutMeDLOG, nil, (WindowPtr) -1);
  177.     SetPort(theDialog);
  178.  
  179.     do {
  180.         ModalDialog(nil, &itemHit);
  181.     } while (itemHit != okButton);
  182.  
  183.     CloseDialog(theDialog);
  184.  
  185.     SetPort(savePort);
  186.     return;
  187. }
  188.  
  189. /*______________________________________________________*/
  190. /*                 Do Menu Function                     */
  191. /*______________________________________________________*/
  192. void doCommand(mResult)
  193.     long    mResult;
  194. {
  195.     int                     theMenu, theItem;
  196.     char                    daName[256];
  197.     GrafPtr                 savePort;
  198.  
  199.     theItem = LoWord(mResult);
  200.     theMenu = HiWord(mResult);
  201.     
  202.     switch (theMenu) {
  203. /*______________________________________________________*/
  204. /*                    Do Apple Menu                     */
  205. /*______________________________________________________*/
  206.         case appleID:
  207.             if (theItem == aboutMeCommand)
  208.                 showAboutMeDialog();
  209.             else {
  210.                 GetItem(mymenu0, theItem, daName);
  211.                 GetPort(&savePort);
  212.                 (void) OpenDeskAcc(daName);
  213.                 SetPort(savePort);
  214.             }
  215.             break;
  216. /*______________________________________________________*/
  217. /*                     Do File Menu                     */
  218. /*______________________________________________________*/
  219.         case fileID:
  220.             switch (theItem) {
  221.                 case startCommand:
  222.                     start();
  223.                     break;
  224.                 case printCommand:
  225.                     Print();
  226.                     break;
  227.                 case quitCommand:
  228.                     DoneFlag = TRUE;
  229.                     break;
  230.                 default:
  231.                     break;
  232.                 }
  233.             break;
  234. /*______________________________________________________*/
  235. /*                     Do Edit Menu                     */
  236. /*______________________________________________________*/
  237.         case editID:
  238.             switch (theItem) {
  239.                 default:
  240.                     break;
  241.                 }
  242.             break;
  243.         default:
  244.             break;
  245.         }
  246.     HiliteMenu(0);
  247.     return;
  248. }
  249.  
  250. /*______________________________________________________*/
  251. /*               Initialization traps                   */
  252. /*______________________________________________________*/
  253. void init()
  254. {
  255.     RgnHandle        tempRgn;
  256.     
  257.     UnloadSeg(_DataInit);
  258.     InitGraf(&qd.thePort);
  259.     FlushEvents(everyEvent, 0);
  260.     InitWindows();
  261.     InitDialogs(nil);
  262.     InitCursor();
  263. /*______________________________________________________*/
  264. /*            If not right Machine then stop            */
  265. /*______________________________________________________*/
  266. /*    OSys = SysEnvirons(VERSION,&theWorld);
  267.     if(!theWorld.hasColorQD)
  268.         Color = TRUE;
  269.     else
  270.         Color = FALSE;
  271.         
  272.     if(theWorld.processor >= env68020)
  273.         CPU20 = TRUE;
  274.     else
  275.         CPU20 = FALSE;
  276.         
  277.     if(!theWorld.hasFPU)
  278.         FPU = TRUE;
  279.     else
  280.         FPU = FALSE;
  281.         
  282.     if(theWorld.systemVersion < 0x0600)
  283.         Sys6 = TRUE;
  284.     else
  285.         Sys6 = FALSE;
  286.         
  287.     if (CPU20 && (GetTrapAddress(0xA06E) != GetTrapAddress(0xA89F))) {
  288.         mySpBlockPtr = &mySpBlock;
  289.         mySpBlockPtr->spSlot = 0;
  290.         mySpBlockPtr->spTBMask = 0x03;
  291.         mySpBlockPtr->spCategory = 9;
  292.         mySpBlockPtr->spCType = 2;
  293.                 
  294.         err = SNextTypeSRsrc (mySpBlockPtr);
  295.                 
  296.         if (err) {
  297.             mySpBlockPtr = &mySpBlock;
  298.             mySpBlockPtr->spSlot = 0;
  299.             mySpBlockPtr->spTBMask = 0x03;
  300.             mySpBlockPtr->spCategory = 9;
  301.             mySpBlockPtr->spCType = F;
  302.                 
  303.             err = SNextTypeSRsrc (mySpBlockPtr);
  304.                 
  305.             if (err)
  306.                 Memboard = FALSE;
  307.             else {
  308.                 Memboard = TRUE;
  309.                 WhereBoard = mySpBlockPtr->spSlot;
  310.                 }
  311.             }
  312.         else {
  313.             Memboard = TRUE;
  314.             WhereBoard = mySpBlockPtr->spSlot;
  315.             }
  316.         }*/
  317.                     
  318. /*______________________________________________________*/
  319. /*                     Set Rects                        */
  320. /*______________________________________________________*/
  321.     screenRect = qd.screenBits.bounds;
  322.     offLeft = 0;
  323.     offTop = 0;
  324.     offRight = screenRect.right;
  325.     offBottom = screenRect.bottom;
  326.     SetRect(&BaseRect, 40, 60, 472, 282);
  327.     tempRgn = GetGrayRgn();
  328.     HLock ((Handle) tempRgn);
  329.     TotalRect = (**tempRgn).rgnBBox;
  330.     SetRect(&minRect, 80, 80, (**tempRgn).rgnBBox.right - 40, 
  331.                 (**tempRgn).rgnBBox.bottom - 40);
  332.     HUnlock ((Handle) tempRgn);
  333.  
  334. /*______________________________________________________*/
  335. /*        Open Window & set Palette & Picture           */
  336. /*______________________________________________________*/
  337.     theGDevice = GetMainDevice();
  338.     HLock ((Handle) theGDevice);
  339.     mycolors = (**(**theGDevice).gdPMap).pmTable;
  340.     numcolor = (**(**theGDevice).gdPMap).pixelSize;
  341.     HUnlock((Handle) theGDevice);
  342.     switch (numcolor) {
  343.         case 1:
  344.             numcolor = 2;
  345.             break;
  346.         case 2:
  347.             numcolor = 4;
  348.             break;
  349.         case 4:
  350.             numcolor = 16;
  351.             break;
  352.         case 8:
  353.             numcolor = 256;
  354.             break;
  355.         }
  356.     
  357.     myWindow = NewCWindow(nil, &BaseRect, "", TRUE, zoomDocProc, 
  358.                             (WindowPtr) -1, TRUE, 150);
  359.     SetPort((WindowPtr) myWindow);
  360.     DrawGrowIcon (myWindow);
  361.  
  362.     srcPalette = NewPalette (numcolor, mycolors, pmCourteous, 0);
  363.     SetPalette ((WindowPtr) myWindow, srcPalette, TRUE);
  364.     
  365. /*______________________________________________________*/
  366. /*                    Set menus                         */
  367. /*______________________________________________________*/
  368.     mymenu0 = GetMenu(appleID);
  369.     AddResMenu(mymenu0, 'DRVR');
  370.     InsertMenu(mymenu0,0);
  371.     mymenu1 = newmenu(129,"File");
  372.     appendmenu(mymenu1,"Start;-;Print;-;Quit");
  373.     InsertMenu(mymenu1,0);
  374.     mymenu2 = newmenu(130,"Edit");
  375.     InsertMenu(mymenu2,0);
  376.     DrawMenuBar();
  377.  
  378. /*______________________________________________________*/
  379. /*                  Init variables                      */
  380. /*______________________________________________________*/
  381.     DoneFlag = FALSE;
  382.     yieldTime = 0;
  383.     return;
  384. }
  385.  
  386. main()
  387. {
  388.     Boolean        track;
  389.     long        growResult;
  390. /*______________________________________________________*/
  391. /*                   Main Event loop                    */
  392. /*______________________________________________________*/
  393.     init();
  394.     for ( ;; ) {
  395.         if (DoneFlag) {
  396.             ExitToShell();
  397.             }
  398.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  399.             switch (myEvent.what) {
  400.                 case mouseDown:
  401.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  402.                         case inSysWindow:
  403.                             SystemClick(&myEvent, whichWindow);
  404.                             break;
  405.                         case inMenuBar:
  406.                             doCommand(MenuSelect(myEvent.where));
  407.                             break;
  408.                         case inContent:
  409.                             break;
  410.                         case inDrag:
  411.                             DragWindow (whichWindow, myEvent.where, &TotalRect);
  412.                             EraseRect (&whichWindow->portRect);
  413.                             DrawGrowIcon (whichWindow);
  414.                             break;
  415.                         case inGrow:
  416.                             growResult = GrowWindow (whichWindow, myEvent.where,
  417.                                                     &minRect);
  418.                             SizeWindow(whichWindow, LoWord(growResult), 
  419.                                     HiWord(growResult), TRUE);
  420.                             EraseRect (&whichWindow->portRect);
  421.                             DrawGrowIcon (whichWindow);
  422.                             break;
  423.                         case inGoAway:
  424.                             track = TrackGoAway (whichWindow, myEvent.where);
  425.                             if (track)
  426.                                 CloseWindow (whichWindow);
  427.                             break;
  428.                         case inZoomIn:
  429.                             track = TrackBox (whichWindow, myEvent.where, inZoomIn);
  430.                             if (track) {
  431.                                 ZoomWindow (whichWindow, inZoomIn, TRUE);
  432.                                 EraseRect (&whichWindow->portRect);
  433.                                 DrawGrowIcon (whichWindow);
  434.                                 }
  435.                             break;
  436.                         case inZoomOut:
  437.                             track = TrackBox (whichWindow, myEvent.where, inZoomOut);
  438.                             if (track) {
  439.                                 ZoomWindow (whichWindow, inZoomOut, TRUE);
  440.                                 EraseRect (&whichWindow->portRect);
  441.                                 DrawGrowIcon (whichWindow);
  442.                                 }
  443.                             break;
  444.                         default:
  445.                             break;
  446.                         }
  447.                     break;
  448.                 case keyDown:
  449.                     break;
  450.                 case autoKey:
  451.                     break;
  452.                 case updateEvt:
  453.                     if ((WindowPtr) myEvent.message == myWindow) {
  454.                         BeginUpdate((WindowPtr) myWindow);
  455.                         EndUpdate((WindowPtr) myWindow);
  456.                         }
  457.                     break;
  458.                 case diskEvt:
  459.                     break;
  460.                 case activateEvt:
  461.                     break;
  462.                 case app4Evt:
  463.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  464.                         yieldTime = 30;
  465.                         HideWindow((WindowPtr) myWindow);
  466.                         }
  467.                     else { /* Resume */
  468.                         yieldTime = 0;
  469.                         ShowWindow((WindowPtr) myWindow);
  470.                         SetPort((WindowPtr) myWindow);
  471.                         }
  472.                     break;
  473.                 default:
  474.                     break;
  475.                 }
  476.             }
  477.         }
  478. }